package com.hshit.downloader.custom;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hshit.downloader.R;

public class LoadMoreListView extends LinearLayout {
	private View view;
	
	private ListView mListView;
	private LinearLayout mProgressBarHolder;
	private TextView mTxtLoader;
	private TextView mTvError;
	private RelativeLayout mErrorHolder;
	private boolean showing;
	
	public LoadMoreListView(Context context) {
		super(context);
		init();
	}
	public LoadMoreListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	
	private void init() {
		LayoutInflater inflater = (LayoutInflater) 
				getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		view = inflater.inflate(R.layout.layout_load_more_listview, null);
		mListView = (ListView) view.findViewById(R.id.lv_main_list);
		mProgressBarHolder = (LinearLayout) view.findViewById(R.id.progress_holder);
		mTxtLoader = (TextView) mProgressBarHolder.findViewById(R.id.tv_loader);
		mTvError = (TextView) view.findViewById(R.id.tv_error);
		mErrorHolder = (RelativeLayout) view.findViewById(R.id.error_holder);
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		addView(view);
	}
	
	public boolean isLoading() {
		return showing;
	}
	
	public void progressBar(boolean loading) {
		showing = loading;
		mProgressBarHolder.setVisibility(loading ? View.VISIBLE : View.GONE);
		if (loading) {
			Animation inAnim = AnimationUtils.loadAnimation(
					getContext(), R.anim.slide_in_from_bottom);
			mProgressBarHolder.startAnimation(inAnim);
		} else {
			Animation outAnim = AnimationUtils.loadAnimation(
					getContext(), R.anim.slide_out_to_bottom);
			mProgressBarHolder.startAnimation(outAnim);
		}
		// mProgressBarHolder.setVisibility(loading ? View.VISIBLE : View.GONE);
	}
	
	public void showError(String text) {
		mErrorHolder.setVisibility(View.VISIBLE);
		mTvError.setText(text);
	}
	public void hideError() {
		mErrorHolder.setVisibility(View.GONE);
	}
	
	public void setText(String text) {
		mTxtLoader.setText(text);
	}
	public ListView getListView() {
		return mListView;
	}
	public void setListView(ListView mListView) {
		this.mListView = mListView;
	}
	public LinearLayout getProgressBarHolder() {
		return mProgressBarHolder;
	}
	public void setProgressBarHolder(LinearLayout mProgressBarHolder) {
		this.mProgressBarHolder = mProgressBarHolder;
	}
}